<!--扫码授权-->
<template>
  <div class="bg bg2">

    <div class="logo">
      <LImage class="logo-image " :src="config.img('view_btn_workshopLogin_black.png')"></LImage>
    </div>

    <div class="height60"></div>
    <text class="font36 text1 text-center">{{T(data.title)}}</text>

    <div class="heigh20"></div>
    <text class="font30 warning-t1 text-center all">{{warn}}</text>
    
    <div class="all"></div>
    <div class="ok-but main1" v-if="warn" @click="scanClick()">
      <text class="font30 text1">{{T('重新扫码')}}</text>
    </div>
    <div v-else>
      <div class="ok-but main1" @click="okClick">
        <text class="font30 text1">{{T(data.ok)}}</text>
      </div>
      <div class="ok-but bg3" @click="closeClick">
        <text class="font30 text2">{{T('取消')}}</text>
      </div>
    </div>
    <div class="height60"></div>
  </div>
</template>

<script>

//功能
import navigator from "@/_js/navigator"; 
import config from "@/_js/config"; 
import modal from "@/_js/modal"; 
import API from "@/_js/api";

const weexFunction = weex.requireModule("WeexFunction");
import weexHttp from "@/_js/https"; ;

import I18N from "@/_i18n/i18n";
var T = I18N.t;

weexFunction.setTitle('Vtrading');
export default {

  data() {
    return {
      T:T,
      config:config,
      isScan: false,
      
      typed:{'login':{'title':'web策略工坊登录确认', 'ok':'确认登录'},
            'vtradingPcLogin':{'title':'PC端登录确认', 'ok':'确认登录'},
            'trade':{'title':'web策略工坊交易确认', 'ok':'确认交易'},

            'trustFundLogin':{'title':'托管平台登录确认', 'ok':'确认登录'},
            'bond':{'title':'保证金划转确认', 'ok':'确认划转'}},

      data:{'title':'Vtrading 扫码授权', 'ok':'确认'}, 
      warn:'', //警告

      type:undefined, //login登录 trade交易
      temporaryToken:undefined, //授权码
    };
  },

  mounted(){

    var str = navigator.getParam("str");
    this.verifyCode(str);
  },

  methods:{ 
    
    /********************************************** HTTP*/
    //http授权登录-工坊
    httpGrantLog() {

      modal.showLoad();
      var param = {'temporaryToken':this.temporaryToken};
      weexHttp.postX("userV2/authorizedLanding",param,true,(data)=>{
        modal.hideLoad();
        if(data.status == 200){

          data = data.data;
          var msg = data.result;
          if (msg && msg.length) {
            this.warn = msg;
          } else {
            modal.toast(T('登录授权成功'));
            this.closeClick();
          }
        } else {
          modal.toast(data.msg);
        }
      });
    },

    //http授权登录-pc
    httpGrantLogPC() {

      modal.showLoad();
      var param = {'temporaryToken':this.temporaryToken};
      weexHttp.postX("userV2/authorizedPcLanding",param,true,(data)=>{
        modal.hideLoad();
        if(data.status == 200){

          data = data.data;
          var msg = data.result;
          if (msg && msg.length) {
            this.warn = msg;
          } else {
            modal.toast(T('登录授权成功'));
            this.closeClick();
          }
        } else {
          modal.toast(data.msg);
        }
      });
    },

    //http授权登录PC
    httpPcLog() {
      modal.showLoad();
      var param = {'temporaryToken':this.temporaryToken};
      weexHttp.postX("userV2/authorizedLanding",param,true,(data)=>{
        modal.hideLoad();
        if(data.status == 200){
          data = data.data;
          var msg = data.result;
          if (msg && msg.length) {
            this.warn = msg;
          } else {
            modal.toast(T('登录授权成功'));
            this.closeClick();
          }
        } else {
          modal.toast(data.msg);
        }
      });
    },

    //http获取授权交易信息
    httpGetTradeInfo() {

      modal.showLoad();
      var param = {'temporaryToken':this.temporaryToken};
      weexHttp.postX("strategyV2/getEmpowerParam",param,true,(data)=>{
        if(data.status == 200){
          
          data = data.data;
          var msg = data.result;
          if (msg && msg.length) {
            modal.hideLoad();
            this.warn = msg;
          } else {
            this.verifyApi(data, 0);
          }
        } else {
          modal.hideLoad();
          this.warn = T('网络异常，请重新扫码确认');
        }
      });
    },

    //获取Web服务器秘钥
    httpPublicKey(info) {
      
      var param = {'serverType':'webStrategyServer'};
      weexHttp.post("assetsV2/getPublicKey",param,true,(data)=>{
        
        if(data.status == 200){
          data = data.data;
          var key = data['publicKey'];
          this.rsaApi(info, 0, key);
        } else {
          modal.hideLoad();
          this.warn = T('网络异常，请重新扫码确认');
        }
      });
    },

    //授权交易
    httpGrantTrade(info) {

      var param = {'temporaryToken':this.temporaryToken,
                    'data':JSON.stringify(info)};

      weexHttp.postX("strategyV2/empowerStrategyStart",param,true,(data)=>{
        
        modal.hideLoad();
        if(data.status == 200){
          modal.toast(T('交易授权成功'));
          this.closeClick();
        } else {
          this.warn = T('网络异常，请重新扫码确认');
        }
      });
    },

    //授权划转保证金
    httpGrantMargin() {
      
      modal.showLoad();
      var param = {'temporaryToken':this.temporaryToken};
      weexHttp.postX("wallet/marginDeduction",param,true,(data)=>{
        
        modal.hideLoad();
        if(data.status == 200){

          data = data.data;
          var msg = data.result;
          if (msg && msg.length) {
            this.warn = msg;
          } else {
            modal.toast(T('量化托管保证金划转成功'));
            this.closeClick();
          }
        } else {
          this.warn = T('网络异常，请重新扫码确认');
        }
      });
    },

    /********************************************** 其他方法*/
    //校验二维码
    verifyCode(str) {

      var param ;
      try {
        param = JSON.parse(str);
      } catch (error) {
        this.warn = T('非授权二维码!');
        return;
      }

      if (param == undefined ||
          param.type == undefined || param.type.length == 0 ||
          param.temporaryToken == undefined || param.temporaryToken.length == 0) {
        this.warn = T('非授权二维码!');
        return;
      }

      this.type = param.type;
      this.temporaryToken = param.temporaryToken;
      this.data = this.typed[this.type];
      this.warn = '';
    },

    //校验api
    verifyApi(info, index) {
      
      //轮询完成
      if (index >= info.tradePairs.length) {
        this.httpPublicKey(info);
        return;
      }

      var pair = info.tradePairs[index];
      API.getApi(pair.apiId, 'all', (api)=>{

        if (api == undefined) {
          modal.hideLoad();
          var error = '“' + pair.apiName + '”' + T('API未保存在本手机，无法授权');
          this.warn = error;
        } else if (api.isExpire) {
          modal.hideLoad();
          var error = '“' + pair.apiName + '”' + T('API已过期，无法使用');
          this.warn = error;
        } else {
          pair.api = api;
          index = index + 1;
          this.verifyApi(info, index);
        }
      });
    },

    //rsa加密api
    rsaApi(info, index, key) {

      //轮询完成
      if (index >= info.tradePairs.length) {
        this.httpGrantTrade(info);
        return;
      }

      var pair = info.tradePairs[index];
      API.rsaApi(pair.api, key, (rsa)=>{
        pair.api = rsa;
        index = index + 1;
        this.rsaApi(info, index, key)
      });
    },

    /********************************************** 点击*/
    //关闭
    closeClick() {
      navigator.back();
    },

    //确定
    okClick() {
      
      //授权登录
      if (this.type == 'login' || this.type == 'trustFundLogin') {
        this.httpGrantLog();
        return;
      }

      if (this.type == 'vtradingPcLogin') {
        this.httpGrantLogPC();
        return;
      }


      //授权交易
      if (this.type == 'trade') {
        this.httpGetTradeInfo();
        return;
      }

      //授权交易
      if (this.type == 'bond') {
        this.httpGrantMargin();
        return;
      }
    },

    //重新扫码
    scanClick() {

      if (this.isScan) return;

      this.isScan = true;
      setTimeout(() => {
        this.isScan = false;
      }, 1000);


      weexFunction.scanCode((str)=>{
        this.verifyCode(str);
      });
    },
  }
}
</script>



<style src="@/_css/style.css"></style>
<style scoped>
.bg {
  flex: 1;

  padding-left: 40px;
  padding-right: 40px;
}
.close-image  {
  position: absolute;
  width: 36px;
  height: 36px;

  left: 50px;
  top: 50px;
}

.logo{
  flex-direction: row;
  justify-content: center;
}
.logo-image {
  width: 100px;
  height: 100px;
  margin-top: 250px;
}

.text-center {
  text-align: center;
}

.ok-but{
  justify-content: center;
  align-items: center;
  height: 70px;
  border-radius: 4px;

  margin-bottom: 30px;
}

/*********************间距 */
.heigh20 {
  height: 20px;
}
.height60 {
  height: 60px;
}
</style>
